package com.demo.java.OD351_400.OD380;

import java.util.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【模拟消息队列(B卷)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146238536
 */
public class OdMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取发送者数据
        String line1 = scanner.nextLine().trim();
        String[] words1 = line1.split(" ");
        int len = words1.length / 2;
        int[][] senders = new int[len][2];
        for (int i = 0; i < len; i++) {
            senders[i][0] = Integer.parseInt(words1[i * 2]); // 发送时间
            senders[i][1] = Integer.parseInt(words1[i * 2 + 1]); // 消息内容
        }

        // 读取接收者数据
        String line2 = scanner.nextLine().trim();
        String[] words2 = line2.split(" ");
        int len2 = words2.length / 2;
        int[][] receivers = new int[len2][2];
        for (int i = 0; i < len2; i++) {
            receivers[i][0] = Integer.parseInt(words2[i * 2]); // 接收开始时间
            receivers[i][1] = Integer.parseInt(words2[i * 2 + 1]); // 接收结束时间
        }

        // 按发送时间对发送者进行排序
        Arrays.sort(senders, (a, b) -> a[0] - b[0]);

        // 初始化接收者的消息列表
        ArrayList<Integer>[] ans = new ArrayList[len2];
        for (int i = 0; i < len2; i++) {
            ans[i] = new ArrayList<>();
        }

        // 处理消息传递
        for (int[] sender : senders) {
            int sendTime = sender[0];
            int message = sender[1];
            for (int j = len2 - 1; j >= 0; j--) {
                int startTime = receivers[j][0];
                int endTime = receivers[j][1];
                if (startTime <= sendTime && sendTime < endTime) {
                    ans[j].add(message); // 如果接收者可以接收消息，加入消息
                    break;
                }
            }
        }

        // 输出结果
        for (int i = 0; i < len2; i++) {
            ArrayList<Integer> list = ans[i];
            if (list.isEmpty()) {
                System.out.println(-1); // 如果没有消息接收，输出 -1
            } else {
                StringJoiner joiner = new StringJoiner(" ");
                for (int message : list) {
                    joiner.add(String.valueOf(message));
                }
                System.out.println(joiner.toString()); // 输出接收到的消息
            }
        }

        // 关闭Scanner
        scanner.close();
    }
}